package com.fjrz.bbxwj.mode.vm

import android.app.Application
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.MutableLiveData
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.UserMeResponse
import com.fjrz.bbxwj.entity.UserSmsResponse
import com.fjrz.bbxwj.mode.api.userLogin
import com.fjrz.bbxwj.mode.api.userSmsSend
import com.module.lemlin.view.LoginAuthPopup
import io.reactivex.rxjava3.disposables.Disposable
import org.greenrobot.eventbus.EventBus

class AccessViewModel(application: Application) : BaseViewModel(application) {

    val userSmsLiveData = MutableLiveData<HttpServiceResponse<UserSmsResponse>>()
    val userMeLiveData = MutableLiveData<HttpServiceResponse<UserMeResponse>>()

    /**
     * 发送短信验证码
     *
     * @param mobile 手机号
     * @param event 事件：login,set_pwd
     * */
    fun userSmsSend(mobile: String, event: String) {
        val listener = object : ViewModelSubscribe<UserSmsResponse>(userSmsLiveData) {
            override fun onSuccess(resp: UserSmsResponse) {
                super.onSuccess(resp.apply { data = mobile })
            }
        }
        val disposable: Disposable =
            userSmsSend(mobile, event, listener)
        dispose.add(disposable)
    }

    /**
     * 短信验证码登录
     *
     * @param mobile 手机号
     * @param smsCode 短信验证码
     * */
    fun userLogin(mobile: String, smsCode: String) {
        val listener = object : ViewModelSubscribe<UserMeResponse>(userMeLiveData) {
            override fun onSuccess(resp: UserMeResponse) {
                super.onSuccess(resp)
                LoginAuthPopup.hide()
                EventBus.getDefault().post(LoginAuthPopup.Action.ACTION_LOGIN_SUCCESS)
            }
        }
        val disposable: Disposable =
            userLogin(mobile, smsCode, listener)
        dispose.add(disposable)
    }

    val contentCheckCodeLiveData = MutableLiveData<CharSequence>()
    val contentMobileLoginLiveData = MutableLiveData<CharSequence>()
    val contentMobileBindLiveData = MutableLiveData<CharSequence>()


    fun observableEditText(view: EditText) {
        view.addTextChangedListener {
            when (view.id) {
                R.id.etLoginMobileContent -> contentMobileLoginLiveData.postValue(
                    if (it.isNullOrEmpty()) null else it.toString()
                )
                R.id.etBindMobileContent -> contentMobileBindLiveData.postValue(
                    if (it.isNullOrEmpty()) null else it.toString()
                )
                R.id.etCheckCodeContent -> contentCheckCodeLiveData.postValue(
                    if (it.isNullOrEmpty()) null else it.toString()
                )
            }
        }
    }

    fun clearLiveDataCheckCode() {
        this.contentCheckCodeLiveData.postValue(null)
    }

    fun clearLiveDataMobileLogin() {
        this.contentMobileLoginLiveData.postValue(null)
    }

    fun clearLiveDataMobileBind() {
        this.contentMobileBindLiveData.postValue(null)
    }
}